import com.guojin.pojo.Dao.IUserDao;
import com.guojin.pojo.User;
import guojin.io.Resource;
import guojin.sqlsession.SqlSession;
import guojin.sqlsession.SqlSessionFactory;
import guojin.sqlsession.SqlSessionFactoryBuilder;
import jdk.nashorn.internal.ir.CallNode;
import org.junit.Test;

import java.io.InputStream;
import java.sql.*;
import java.util.List;

/**
 * @author haoge
 * @version $Id test.java, v 0.1 2021-06-09 0:26 haoge Exp $$
 */
public class test {
    @Test
    public void InputStreamIPersiset() throws Exception {

        InputStream inputStream = Resource.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        User user = new User();
        user.setId(1);
        user.setUsername("张三");
        //       List<User> objects = sqlSession.selectList("user.selectList");
//
//     //   User o = sqlSession.selectOne("user.selectOne", user);
//        System.out.println(objects);
//        IUserDao mapper = sqlSession.getMapper(IUserDao.class);
//        User byCondition = mapper.findByCondition(user);
//        System.out.println(byCondition);
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);

        List<User> all = userDao.findAll();

        for (User user1 : all) {
            System.out.println(user1);
        }
    }


    //        String url = "jdbc:mysql:///whr";
//        String username = "root";
//        String password = "root";
//        Connection conn = null;
//        Connection connection = null;
//        PreparedStatement preparedStatement = null;
//        ResultSet resultSet = null;
//        String driver = "com.mysql.jdbc.Driver";
//        String dbName = "whr";
//        String passwrod = "root";
//        String userName = "root";
//        String url = "jdbc:mysql:///" + dbName;
//        String sql = "select * from user";
//
//        try {
//            Class.forName(driver);
//            Connection conn = DriverManager.getConnection(url, userName,
//                    passwrod);
//            PreparedStatement ps = conn.prepareStatement(sql);
//            ResultSet rs = ps.executeQuery();
//            while (rs.next()) {
//                System.out.println("id : " + rs.getInt(1) + " name : "
//                        + rs.getString(2));
//            }
//
//            // 关闭记录集
//            if (rs != null) {
//                try {
//                    rs.close();
//                } catch (SQLException e) {
//                    e.printStackTrace();
//                }
//            }
//
//            // 关闭声明
//            if (ps != null) {
//                try {
//                    ps.close();
//                } catch (SQLException e) {
//                    e.printStackTrace();
//                }
//            }
//
//            // 关闭链接对象
//            if (conn != null) {
//                try {
//                    conn.close();
//                } catch (SQLException e) {
//                    e.printStackTrace();
//                }
//            }
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//    }
    @Test
    public void InputStreamIPersiset2() throws Exception {

        InputStream inputStream = Resource.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        User user = new User();
          user.setId(8);
        user.setUsername("小李");

        IUserDao userDao = sqlSession.getMapper(IUserDao.class);

         // int i = userDao.addUser(user);
        //  System.out.println("添加了"+i+"条数据");

        //查询李四2
         //  User byCondition = userDao.findByCondition(user);
        //  System.out.println(byCondition);

        //  int i = userDao.addUser(user);
        // System.out.println("添加了"+i+"条数据");

        //  int i = userDao.updateUser(user);
        //   System.out.println("更改了"+i+"条，id为"+user.getId()+"的数据");

        int i = userDao.deleteUser(user);
        System.out.println("删除了" + i + "条，id为" + user.getId() + "的数据");

    }
}